﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using HouseKeepingPL.HKService;

namespace HouseKeepingPL
{
    /// <summary>
    /// This class is the view model of login
    /// </summary>
    public class LoginViewModel : ViewModelBase
        //, INotifyPropertyChanged
    {
        private static readonly ServicesClient clientProxy = new ServicesClient();

        public LoginViewModel()
        {
            user = new User();
            AuthLogin = new RelayCommand(() => AuthLoginExcecute(), () => true);
            CreateUserClick = new RelayCommand(() => CreateUserClickExcecute(), () => true);
            ExitApplication = new RelayCommand(() => ExitApplicationExcecute(), () => true);
        }

        //public string TextCheck { get; set; }
        private User user;

        public String BindUsername
        {
            get { return user.Username; }
            set { user.Username = value; }
        }

        public String BindPassword
        {
            get { return user.Password; }
            set { user.Password = value; }
        }

        private void CreateUserClickExcecute()
        {
            CreateUserWindow _createUserWindow = new CreateUserWindow();
            _createUserWindow.ShowDialog();
        }

        public ICommand CreateUserClick { get; private set; }
        public ICommand AuthLogin { get; private set; }
        public ICommand ExitApplication { get; private set; }

        /// <summary>
        /// Authenticate login infront of WCF service in the server
        /// </summary>
        private void AuthLoginExcecute()
        {
            //MessageBox.Show(user.Username + " " + user.Password);
           
            try
            {
                User serviceUser = clientProxy.GetUser(user.Username);

                if (serviceUser.Password == user.Password)
                {
                    //MessageBox.Show("Authenticated, Welcome!");
                    MainWindow mWindow = new MainWindow();
                    mWindow.ShowDialog();
                }
                else
                    MessageBox.Show("You shall not pass!");

            }
            catch ( Exception ex )
            {
                MessageBox.Show("User Does not exists! \n " + ex.Message);
            }

        }

        private void ExitApplicationExcecute()
        {
            Application.Current.Shutdown();
        }

        //public event PropertyChangedEventHandler PropertyChanged;
        //private void OnPropertyChanged (String propertyName)
        //{
        //    if ( PropertyChanged != null )
        //    {
        //        PropertyChanged ( this, new PropertyChangedEventArgs(propertyName) );
        //    }
        //}

        //private readonly User _user;
        //public LoginViewModel()
        //{
        //    //TempCommand = new Command
        //}

        ////System.Windows.Input.ICommand _tempCommand;
        ////public System.Windows.Input.ICommand TempCommand
        ////{
        ////    get 
        ////    {
        ////        return _tempCommand;
        ////    }

        ////    set 
        ////    {
        ////        if (_tempCommand == value) return;
        ////        _tempCommand = value;
        ////        OnProprtyChanged("TempCommand");
        ////    }
        ////}


        //private void OnProprtyChanged(string propName)
        //{
        //    if (PropertyChanged != null)
        //    {
        //        PropertyChanged(this,new PropertyChangedEventArgs(propName));
        //    }
        //}

        //public event PropertyChangedEventHandler PropertyChanged;

   }
}
